CPP:=g++
BUILD:=$(shell pwd)/build/
SRCS_DIR:=$(shell pwd)/
SRCS:=main.cpp driver.cpp lexer.cpp parser.cpp ast.cpp

OBJS:=$(patsubst %.cpp,%.o,$(SRCS))
OBJS:=$(addprefix $(BUILD),$(OBJS))

DEPS:=$(patsubst %.cpp,%.d,$(SRCS))
DEPS:=$(addprefix $(BUILD),$(DEPS))

SRCS:=$(addprefix $(SRCS_DIR),$(SRCS))
LDFLAGS:= -lm
all: anlysis
	make cal

anlysis: lexer.l parser.y
	flex -+ -o lexer.cpp lexer.l
	bison -o parser.cpp parser.y

cal: $(OBJS)
	$(CPP) $(LDFLAGS) -g -o $@ $^

-include $(DEPS)

$(BUILD)%.o: $(SRCS_DIR)/%.cpp
	mkdir -p $$(dirname $@)
	$(CPP) $(CPPFLAGS) $(CFLAGS)  -MM -MT $@ -MF $(patsubst %.o,%.d,$@) $<
	$(CPP) -g -c $(CPPFLAGS) -o $@ $(CFLAGS) $<

.PHONY: clean
clean:
	rm -rf build lexer.cpp parser.cpp parser.hpp position.hh cal location.hh
